/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#include "installer.h"

#include <jb/std/log.h>

Installer::Installer() :
	busy(false),
	lastcode(-1),
	lastApp("none") {
}

Installer::~Installer() {

}

void Installer::install(Version version, const bool dryrun) {
	if (busy)
		throw Busy();
	busy = true;
	runInstaller(version, dryrun);
	busy = false;
}

void Installer::runInstaller(Version app, bool dryrun) {
	std::string command;

	if (!app.hasInstaller()) {
		LOG ("No installer for " << app.name())
		return;
	}

	switch (app.type) {
		case Version::EXE:
			command = std::string("\"") + app.installer() + "\" " + app.parameters();
			break;
		case Version::MSI:
			command = "msiexec /quiet /qb /i \"" + app.installer() + "\" " + app.parameters();
			break;
		case Version::NONE:
			break;
	}
	if (!dryrun) {
		LOG (command)
		lastcode = system(command.c_str());
		LOG ("Result: " << lastcode)
	} else {
		LOG ("Dry run: " << command)
	}
	lastApp = app.name();
}
